import {Request, Response} from "express";
import {checkParams} from "@src/util/util";
import httpStatusCodes from "@src/declarations/major/HttpStatusCodes";
import Constant from "@src/util/constant";
import literatureSql from "@src/sql/literatureSql";
import literatureServer from "@src/services/literatureServer";
import HttpStatusCodes from "@src/declarations/major/HttpStatusCodes";

const paths = {
  add: "/literature",
  delete: "/literature/:id",
  update: "/literature/:id",
  list: "/literature",
  updateRecommend: "/literature/update-recommend/:id",
  updateGrounding: "/literature/update-grounding/:id",
  detail: "/literature-detail/:id",
  ranking: "/literature-ranking",
};

// 添加
const add = async (req: Request, res: Response) => {
  const flag = checkParams(req.body, ["name", "category_id", "file_id", "cover_id"]);
  // 缺乏参数
  if (!flag) {
    res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  } else {
    const data = await literatureServer.add(req);
    res.status(httpStatusCodes.OK).json({data});
  }
};

// 删除
const _delete = async (req: Request, res: Response) => {
  const {id} = req.params;
  if (id === "undefined") {
    return {data: Constant.LACK};
  } else {
    const data = await literatureSql._delete(id);
    return res.status(httpStatusCodes.OK).json({data});
  }
};

// 更新
const update = async (req: Request, res: Response) => {
  // 参数判断
  const flag = checkParams(req.body, ["name", "category_id", "file_id", "cover_id"]);
  const {id} = req.params;
  if (!flag) {
    res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  } else {
    if (id === 'undefined') {
      return {data: Constant.LACK};
    }
    const data = await literatureServer.update(req);
    return res.status(HttpStatusCodes.OK).json({
      data,
    });
  }
};

// 列表
const list = async (req: Request, res: Response) => {
  const page = req.query.page ? req.query.page : 1;
  const size = req.query.size ? req.query.size : 15;
  const filters = req.query.filters ? req.query.filters : {};
  const data = await literatureServer.list(Number(page), Number(size), filters);
  return res.status(data.code).json({data});
};

// 是否首页推荐
const updateRecommend = async (req: Request, res: Response) => {
  const {recommend} = req.body;
  const {id} = req.params;
  if (id === 'undefined' || (!recommend && recommend !== 0)) {
    res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  }
  const data = await literatureServer.updateRecommend(id, recommend);
  return res.status(httpStatusCodes.OK).json({data});
};

// 更新是否上架
const updateGrounding = async (req: Request, res: Response) => {
  const {grounding} = req.body;
  const {id} = req.params;
  if (id === 'undefined' || (!grounding && grounding !== 0)) {
    res.status(httpStatusCodes.BAD_REQUEST).json({
      data: Constant.LACK,
    });
  }
  const data = await literatureServer.updateGrounding(id, grounding);
  return res.status(httpStatusCodes.OK).json({data});
};

// 文献详情
const detail = async (req: Request, res: Response) => {
  const {id} = req.params;
  const data: any = await literatureServer.detail(id);
  return res.status(data.code).json({
    data,
  });
};

// 排名
const ranking = async (req: Request, res: Response) => {
  const data = await literatureSql.ranking();
  return res.status(data.code).json({
    data,
  });
};

export default {
  paths,
  add,
  _delete,
  update,
  list,
  updateRecommend,
  updateGrounding,
  detail,
  ranking,
};